<template>
	<view>
		<u-navbar height="50" back-icon-color="#333" :title="headTitle" title-color="#333" title-size="36"></u-navbar>
		<view class="layoutView">
			<view class="inputView">
				<text>联系人</text>
				<input placeholder="请输入收货人姓名" placeholder-style="color: #999" v-model="username" />
			</view>
			<view class="line"></view>
			<view class="inputView">
				<text>联系方式</text>
				<input placeholder="请输入联系方式" type="number" placeholder-style="color: #999" v-model="userphone" />
			</view>
			<view class="line"></view>
			<view class="labelView">
				<view class="labelTitle">地址标签</view>
				<view v-for="(item,index) in labelList" :key="index" @click="onLabel(index)">
					<view :class="labelIndex == index?'labelTab2':'labelTab'">{{item.name}}</view>
				</view>
			</view>
		</view>
		<view class="layoutView">
			<!-- <view class="inputView" @click="onSetAddress()"> -->
			<view class="inputView" @click="()=>this.showAddress=true">
				<text>所在地区</text>
				<input placeholder="请选择省市区县" placeholder-style="color: #999" disabled v-model="cityData" />
				<u-icon name="arrow-right" color="#666" size="32"></u-icon>
			</view>
			<view class="line"></view>
			<view class="inputView2">
				<text>详细地址</text>
				<textarea placeholder="街道、门牌号等" placeholder-style="color: #999" v-model="address" />
			</view>
		</view>
		<view class="defaultView">
			<view class="defaultTipsView">
				<text class="defaultTitle">设置默认地址</text>
				<text class="defaultText">提醒：每次下单会默认推荐使用该地址</text>
			</view>
			<u-switch v-model="isDefault" size="38" active-color="#f5ce31" inactive-color="#F2F2F2"
				@change="setDefault"></u-switch>
		</view>
		<view class="postView" @click="onSave">保存</view>
		<u-picker v-model="showAddress" mode="region" :params="params" @confirm="selectAddress"></u-picker>

	</view>
</template>

<script>
	import {
		addAddressBtn,
		getAddress,
		editAddress

	} from '@/api/index.js'
	import {
		toast
	} from '../../utils/common.js';
	export default {
		data() {
			return {
				show: false,
				showAddress: false,
				type: 0,
				headTitle: '添加新地址',
				labelIndex: -1,
				labelId: '',
				labelList: [{
						id: 0,
						name: "公司",
					},
					{
						id: 1,
						name: "学校"
					},
					{
						id: 2,
						name: "家"
					}
				],
				addresId: '',
				username: '',
				userphone: '',
				province: '',
				city: '',
				district: '',
				cityData: '',
				address: '',
				isDefault: false,
				lng: "26.5673",
				lat: "106.69071",
				user_id: "",
				params: {
					province: true,
					city: true,
					area: true
				},
			}
		},
		onLoad(e) {
			this.user_id = uni.getStorageSync("user_id")
			this.type = e.type;
			this.addresId = e.id;
			if (this.type == 0) {
				this.headTitle = '添加新地址';
			} else {
				this.headTitle = '编辑地址';
			}
			if (this.addresId) {
				this.initData();
			}
		},
		methods: {
			otherFun(object) {
				console.log(object);
				if (object) {
					this.district = object.obj.ad_info.district
					this.province = object.obj.ad_info.province
					this.lng = object.obj.location.lng
					this.lat = object.obj.location.lat
					this.city = object.obj.ad_info.city
					this.cityData = object.obj.ad_info.province + object.obj.ad_info.city + object.obj.ad_info.district
					this.address = object.obj.address


				} else {
					return
				}
			},
			selectAddress(e) {
				console.log("地址", e);
				this.district = e.area.name
				this.city = e.area.name
				this.province = e.province.name
				this.cityData = e.province.name + e.city.name + e.area.name
			},
			initData() {
				let self = this;
				getAddress(this.addresId).then(res => {
					console.log("获取地址", res);
					self.addresId = res.data.id;
					self.labelId = res.data.lable_id;
					self.username = res.data.contact_person;
					self.userphone = res.data.contact_phone;
					self.province = res.data.province;
					self.city = res.data.city;
					self.district = res.data.district;
					self.cityData = res.data.province + res.data.city + res.data.district;
					self.address = res.data.detail_address;
					self.longitude = res.data.lng;
					self.latitude = res.data.lat;
					self.isDefault = res.data.is_default == 1;
				})
			},
			onLabel(index) {
				this.labelIndex = index;
				this.labelId = this.labelList[index].id;
				console.log("hhh", index, this.labelId);
			},

			setDefault(isDefault) {
				this.isDefault = isDefault;
			},
			onSave() {
				let self = this;
				if (self.username.trim() == '' || self.username == null) {
					toast('请输入收货人姓名')
					return
				}
				if (self.userphone.trim() == '' || self.userphone == null) {
					toast('请输入联系方式')
					return
				}
				if (self.labelId == '' || self.labelId == null) {
					toast('请选择标签')
					return
				}
				if (self.cityData.trim() == '' || self.cityData == null) {
					toast('请选择省市区县')
					return
				}
				if (self.address.trim() == '' || self.address == null) {
					toast('街道、门牌号等')
					return
				}
				if (this.type == 0) {
					let obj = {
						contact_person: this.username,
						contact_phone: this.userphone,
						label_id: this.labelId,
						province: this.province,
						city: this.city,
						district: this.district,
						detail_address: this.address,
						lng: this.lng,
						// lng: this.longitude,
						// lat: this.latitude,
						lat: this.lat,
						is_default: this.isDefault ? 1 : 0
					}
					console.log("添加", obj);
					addAddressBtn(obj).then(res => {
						toast('添加成功')
						console.log(res);
						setTimeout(function() {
							uni.navigateBack({});
						}, 1000);
					})
				} else {
					let obj = {
						delivery_address_id: this.addresId,
						contact_person: this.username,
						contact_phone: this.userphone,
						label_id: this.labelId,
						province: this.province,
						city: this.city,
						district: this.district,
						detail_address: this.address,
						lng: this.longitude,
						lat: this.latitude,
						is_default: this.isDefault ? 1 : 0
					}

					console.log("修改", obj);
					editAddress(obj).then(res => {
						toast('修改成功')
						setTimeout(function() {
							uni.navigateBack();
						}, 500);
					})
				}

			},
			// 地址选择
			goSelectAddress() {
				let that = this
				uni.navigateTo({
					url: '/page/addressSelect/addressSelect'
				})

			},
		}
	}
</script>

<style>
	@import url('add-address');

	page {
		background: #F2F2F2;
	}
</style>